﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using MN.Framework.Common;
using MN.Framework.Common.Service;

namespace MN.Framework.UI
{
    public class Api
    {
        public static void MasterGridColumnsGenerator<EntityType>(GridViewColumnCollection Columns, List<ColumnInfo> entityColumns, RadGridView Sender, BaseEntity Entity, BaseContext Context) where EntityType : BaseEntity
        {
            if (Entity == null)
                Entity = ServiceFactory.Create<EntityType>();

            var smallWidth = Sender.Width / entityColumns.Select(c => (int)c.Size).Sum();

            foreach (var clmn in entityColumns)
            {
                switch (clmn.Type)
                {
                    case ColumnType.Text:
                        GridViewTextBoxColumn txt = new GridViewTextBoxColumn(clmn.Name);
                        txt.HeaderText = clmn.HeaderText;
                        txt.Name = clmn.Name;
                        Columns.Add(txt);
                        break;
                    case ColumnType.Numeric:
                        GridViewMaskBoxColumn mtxt = new GridViewMaskBoxColumn(clmn.Name);
                        mtxt.MaskType = MaskType.Numeric;
                        mtxt.HeaderText = clmn.HeaderText;
                        mtxt.Name = clmn.Name;
                        Columns.Add(mtxt);
                        break;
                    case ColumnType.Boolean:
                        break;
                    case ColumnType.DateTime:
                        break;
                    case ColumnType.PersianDate:
                        break;
                    case ColumnType.Image:
                        break;
                    case ColumnType.Lookup:
                        GridViewComboBoxColumn lookupColumn = new GridViewComboBoxColumn(clmn.Name);                        
                        lookupColumn.DataSource = (clmn as LookupColumnInfo).LookupValues;
                        lookupColumn.ValueMember = "Key";
                        lookupColumn.DisplayMember = "value";
                        lookupColumn.HeaderText = clmn.HeaderText;
                        Columns.Add(lookupColumn);
                        break;
                    case ColumnType.State:
                        break;
                    case ColumnType.EntityCode:
                        break;
                    case ColumnType.Transformer:
                        break;
                    case ColumnType.Refrence:
                        var relatedDS = Context.RelationEntity(Entity.GetType(), clmn.Name);
                        var refInstance = ServiceFactory.CreateRelatedEntity(Entity.GetType(), clmn.Name);
                        #region filter
                        if (string.IsNullOrEmpty(refInstance.DefaultFilterField()))
                        {
                            FilterDescriptor filterComboBox = new FilterDescriptor();
                            filterComboBox.PropertyName = refInstance.DefaultFilterField();
                            filterComboBox.Operator = FilterOperator.Contains;
                            filterComboBox.Value = "";
                            filterComboBox.IsFilterEditor = true;
                            Sender.FilterDescriptors.Add(filterComboBox);
                        }
                        #endregion

                        #region GridViewMultiComboBoxColumn
                        GridViewMultiComboBoxColumn lookup = new GridViewMultiComboBoxColumn(clmn.Name);
                        lookup.DataSource = relatedDS;
                        lookup.HeaderText = clmn.HeaderText;
                        lookup.DisplayMember = refInstance.FirstOrDefaultDisplayField();
                        lookup.ValueMember = "ID";
                        lookup.FieldName = clmn.Name;
                        lookup.Name = clmn.Name;
                        lookup.CustomDataOperation = CustomDataOperation.Filtering;
                        lookup.AllowFiltering = true;
                        lookup.Width = Sender.Width / 2;
                        lookup.StretchVertically = true;
                        lookup.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        Columns.Add(lookup);

                        GridViewTextBoxColumn hiddenText = new GridViewTextBoxColumn("hidden_" + clmn.Name);
                        hiddenText.IsVisible = false;
                        Columns.Add(hiddenText);

                        //lookup.FilterDescriptor = filterComboBox;                    
                        //lookup.AutoCompleteMode = AutoCompleteMode.Suggest;
                        #endregion
                        break;
                    default:
                        break;
                }
                Columns.Last().ReadOnly = clmn.ReadOnly;
                Columns.Last().Width = clmn.Width(smallWidth);
            }
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            //btn.Text = "ذخیره تغییرات";
            //btn.ContextMenuStrip.Click += ContextMenuStrip_Click;
            //Columns.Add(btn);
        }
        public static void MasterGridColumnsGenerator<EntityType>(GridViewColumnCollection Columns, RadGridView Sender, BaseEntity Entity, BaseContext Context) where EntityType : BaseEntity
        {
            if (Columns.Any())
                return;
            if (Entity == null)
                Entity = ServiceFactory.Create<EntityType>();

            var entityColumns = ServiceFactory.GetColumns(Entity).Where(c => c.Type != ColumnType.Image).ToList();
            MasterGridColumnsGenerator<EntityType>(Columns, entityColumns, Sender, Entity, Context);

        }
        public static void DetailGridColumnsGenerator(GridViewColumnCollection Columns, int BaseWidth, BaseEntity Entity, BaseContext Context)
        {
            if (Columns.Any())
                return;
            IEnumerable<ColumnInfo> columns = ServiceFactory.GetColumns(Entity).Where(c => c.Type != ColumnType.Image);

            var smallWidth = BaseWidth / columns.Where(c => c.Type != ColumnType.Image).Select(c => (int)c.Size).Sum();

            foreach (var clmn in columns)
            {
                //if (clmn.Type == ColumnType.Image)
                //{
                //    GridViewImageColumn img = new GridViewImageColumn(clmn.Name);
                //    img.HeaderText = clmn.HeaderText;
                //    img.ImageLayout = ImageLayout.Stretch;
                //    img.ImageAlignment = System.Drawing.ContentAlignment.MiddleCenter;
                //    img.NullValue = ResourceImages.user;
                //    Columns.Add(img);
                //}
                if (clmn.Type == ColumnType.Refrence)
                {
                    #region GridViewMultiComboBoxColumn
                    var relatedDS = Context.RelationEntity(Entity.GetType(), clmn.Name);
                    var refInstance = ServiceFactory.CreateRelatedEntity(Entity.GetType(), clmn.Name);
                    GridViewMultiComboBoxColumn lookup = new GridViewMultiComboBoxColumn(clmn.Name);
                    lookup.DataSource = relatedDS;
                    lookup.DisplayMember = clmn.HeaderText;
                    lookup.ValueMember = "ID";
                    lookup.Name = clmn.Name;
                    lookup.FieldName = clmn.Name;
                    lookup.CustomDataOperation = CustomDataOperation.Filtering;
                    lookup.AllowFiltering = true;
                    lookup.Width = BaseWidth / 2;
                    lookup.StretchVertically = true;
                    Columns.Add(lookup);

                    //lookup.FilterDescriptor = filterComboBox;                    
                    //lookup.AutoCompleteMode = AutoCompleteMode.Suggest;
                    #endregion

                }
                else if (clmn.Type == ColumnType.Numeric)
                {
                    GridViewMaskBoxColumn mtxt = new GridViewMaskBoxColumn(clmn.Name);
                    mtxt.MaskType = MaskType.Numeric;
                    mtxt.HeaderText = clmn.HeaderText;
                    mtxt.Name = clmn.Name;
                    Columns.Add(mtxt);
                }
                else
                {
                    GridViewTextBoxColumn txt = new GridViewTextBoxColumn(clmn.Name);
                    txt.HeaderText = clmn.HeaderText;
                    txt.Name = clmn.Name;
                    Columns.Add(txt);
                }
                //---check readonly----              
                Columns.Last().ReadOnly = clmn.ReadOnly;
                Columns.Last().Width = clmn.Width(smallWidth);
            }
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            //btn.Text = "ذخیره تغییرات";
            //btn.ContextMenuStrip.Click += ContextMenuStrip_Click;
            //Columns.Add(btn);
        }
        public static void DetailGridColumnsGenerator<EntityType>(GridViewColumnCollection Columns, int BaseWidth, BaseEntity Entity, BaseContext Context) where EntityType : BaseEntity
        {
            Entity = ServiceFactory.Create<EntityType>();
            DetailGridColumnsGenerator(Columns, BaseWidth, Entity, Context);
        }
    }
}
